package com.bw.module_main.ui.guide

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_image.GlideApp
import com.bw.lib_storage.AppMMKV
import com.bw.module_main.R
import com.bw.module_main.databinding.ActivityGuideBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.abs

@AndroidEntryPoint
@Route(path = Router.Ui.GuideActivity)
class GuideActivity : BaseActivity<BaseViewModel, ActivityGuideBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_guide
    private val callback: ViewPager2.OnPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            if (position == 2) {
                hideIndicator()
            } else {
                showIndicator()
            }
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            when (position) {
                0 -> {
                    mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                    mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                }
                1 -> {
                    mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                    mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                }
                2 -> {
                    mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                }

            }
        }
    }
    private fun hideIndicator() {
        mBinding.lGuide.visibility = View.INVISIBLE
        mBinding.btnEnter.visibility = View.VISIBLE
    }

    private fun showIndicator() {
        mBinding.lGuide.visibility = View.VISIBLE
        mBinding.btnEnter.visibility = View.INVISIBLE
    }
    override fun initView() {
        super.initView()
        mBinding.vp.adapter = GuidePagerAdapter()
        mBinding.vp.offscreenPageLimit = 1
        //滑动监听:调整指示器
        mBinding.vp.registerOnPageChangeCallback(callback)
        //添加动画效果
        val compositePageTransformer = CompositePageTransformer()
        // 添加一个页面之间的间距
        compositePageTransformer.addTransformer(MarginPageTransformer(40))
        // 添加一个自定义的缩放动画
        compositePageTransformer.addTransformer { page, position ->
            val scale = 0.85f + (1 - abs(position)) * 0.15f
            page.scaleY = scale
            page.scaleX = scale
        }
        mBinding.vp.setPageTransformer(compositePageTransformer)
    }

    override fun initData() {
        super.initData()
    }

    override fun initListener() {
        super.initListener()
        mBinding.btnEnter.setOnClickListener {
            AppMMKV.appGuide = false
            ARouter.getInstance()
                .build(Router.Ui.MainActivity)
                .withTransition(R.anim.right_in, R.anim.left_out)
                .navigation(this)
            ActivityCompat.finishAffinity(this)
        }
    }


    inner class GuidePagerAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
        private val mList = listOf(R.mipmap.bg_guide_1,R.mipmap.bg_guide_2,R.mipmap.bg_guide_3)
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
            val imageView = ImageView(p0.context).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT)
            }
            return object : RecyclerView.ViewHolder(imageView){}
        }

        override fun getItemCount(): Int {
            return mList.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val imageView = holder.itemView as ImageView
            GlideApp.with(imageView.context)
                .load(mList[position])
                .into(imageView)
        }




    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.vp.unregisterOnPageChangeCallback(callback) //解除注册
    }


}